
const operationSheetId = Context.argv.sheetId;
const operationRecordId = Context.argv.id;
let targetRowData = getTargetRecord(operationSheetId, operationRecordId);
//* [测试代码]-start 
// let targetRowData = { '迭代列表': ['202410-2'], '类型': '迭代统计', '数据表名称': '2024迭代需求' }
//* [测试代码]-end
let internalTasks = ['产品-主动规划', '研发技术优化', '其他内部规划'];
main();
//按组获取开发人天
function main() {
    let interationList = targetRowData['迭代列表'];
    let sheetName = targetRowData['数据表名称'];
    let targetSheet = getSheetByName(sheetName);
    let updatedRecords = summaryInteration(targetSheet.id, interationList);
    if (updatedRecords.length > 0) {
        Application.Record.UpdateRecords({
            SheetId: targetSheet.id,
            Records: updatedRecords,
        })
    }
}
function summaryInteration(sheetId, interations) {
    let sheetData = getSheetData(sheetId);
    let updatedRecords = [];
    loop(sheetData, (recordInst) => {
        const fields = recordInst.fields;
        let category = '';
        let validateDepartment = fields['验收部门'];
        let actuateReason = fields['驱动因素'];
        if (!interations.includes(fields['迭代'])) {
            return;
        }
        if (internalTasks.includes(actuateReason)) {
            category = actuateReason;
        } else {
            if (validateDepartment == '客户成功部') {
                category = '客成';
            } else if (['交付','交付赋能组'].includes(validateDepartment)) {
                category = '交付';
            } else if (validateDepartment == '每刻内部') {
                category = '内部规划-外部驱动';
            } else {
                category = '外部-其他'
            }
        }
        updatedRecords.push({
            id: recordInst.id,
            fields: {
                '需求分析类型': category,
            }
        });
    })
    return updatedRecords;
}

/* 以下为通用方法,最新代码在lib目录下;受限于wps脚本运行的环境无法使用引用脚本,所以需要手动去更新以下脚本 */
// 根据数据表名，查找表对象
function getSheetByName(name) {
    let sheets = Application.Sheet.GetSheets();
    let matchedSheet;
    for (let i = 0; i < sheets.length; i++) {
        if (sheets[i].name === name) {
            matchedSheet = sheets[i];
            break;
        }
    }
    if (!matchedSheet) console.warn('The specified table not existed!')
    return matchedSheet;
}

// 获取sheet中的数据
function getSheetData(sheetId) {
    let all = [];
    let offSet = null;
    while (all.length === 0 || offSet) {
        let recordInstance = Application.Record.GetRecords({ SheetId: sheetId, Offset: offSet });
        offSet = recordInstance.offset;
        all = all.concat(recordInstance.records);
    }
    return all;
}


function getTargetRecord(sheetId, recordId) {
    let result = Application.Record.GetRecord({ SheetId: sheetId, RecordId: recordId });
    return result.fields;
}
function loop(list, cb) {
    for (let i = 0; i < list.length; i++) {
        cb(list[i], i);
    }
}
function Loger() {
    let logList = [];
    let log = (value) => {
        logList.push(value)
    }
    let getLog = () => {
        return logList;
    }
    return { log, getLog };
}